package com.ruoyi.modules.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.modules.domain.vo.AppDyAgentUserVo;
import com.ruoyi.modules.service.INumRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.modules.domain.DyAgentUser;
import com.ruoyi.modules.service.IDyAgentUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 代理Controller
 *
 * @author ruoyi
 * @date 2022-01-22
 */
@Api(tags = "后台接口 - 代理")
@RestController
@RequestMapping("/backstage/dyAgentUser")
public class DyAgentUserController extends BaseController {
    @Autowired
    private IDyAgentUserService dyAgentUserService;
    @Autowired
    private INumRecordService numRecordService;
    /**
     * 查询代理列表
     */
    @PreAuthorize("@ss.hasPermi('modules:user:list')")
    @ApiOperation(value="代理列表", notes="代理列表")
    @GetMapping("/list")
    public TableDataInfo list(DyAgentUser dyAgentUser) {
        startPage();
        List<DyAgentUser> list = dyAgentUserService.list(new QueryWrapper<>(dyAgentUser));
        return getDataTable(list);
    }

    /**
     * 导出代理列表
     */
    @PreAuthorize("@ss.hasPermi('modules:user:export')")
    @Log(title = "代理", businessType = BusinessType.EXPORT)
    @ApiOperation(value="导出表格", notes="导出表格")
    @GetMapping("/export")
    public AjaxResult export(DyAgentUser dyAgentUser) {
        List<DyAgentUser> list = dyAgentUserService.list(new QueryWrapper<>(dyAgentUser));
        ExcelUtil<DyAgentUser> util = new ExcelUtil<DyAgentUser>(DyAgentUser.class);
        return util.exportExcel(list, "代理数据");
    }

    /**
     * 获取代理详细信息
     */
    @PreAuthorize("@ss.hasPermi('modules:user:query')")
    @ApiOperation(value="代理详情", notes="代理列情")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(dyAgentUserService.getById(id));
    }


    /**
     * 获取代理详细信息
     */
    @PreAuthorize("@ss.hasPermi('modules:user:query')")
    @ApiOperation(value="代理详情", notes="代理列情")
    @GetMapping(value = "/getAgentUserDetailsVo/{id}")
    public AjaxResult getAgentUserDetailsVo(@PathVariable("id") Long id) {
        AppDyAgentUserVo appDyAgentUserVo = dyAgentUserService.getAppDyAgentUserVo(id);
        return AjaxResult.success(appDyAgentUserVo);
    }



    /**
     * 新增代理
     */
    @PreAuthorize("@ss.hasPermi('modules:user:add')")
    @Log(title = "代理", businessType = BusinessType.INSERT)
    @ApiOperation(value="代理新增", notes="代理新增")
    @PostMapping
    public AjaxResult add(@RequestBody DyAgentUser dyAgentUser) {


        DyAgentUser old = dyAgentUserService.getOne(new LambdaQueryWrapper<DyAgentUser>().eq(DyAgentUser::getUsername, dyAgentUser.getUsername()).last("limit 1"));
        if (old != null) {
            return error("账号用户名重复， 已被占用");
        }

        dyAgentUser.setMoney(BigDecimal.ZERO);
        return toAjax(dyAgentUserService.save(dyAgentUser));
    }

    /**
     * 修改代理
     */
    @PreAuthorize("@ss.hasPermi('modules:user:edit')")
    @Log(title = "代理", businessType = BusinessType.UPDATE)
    @ApiOperation(value="代理修改", notes="代理修改")
    @PutMapping
    public AjaxResult edit(@RequestBody DyAgentUser dyAgentUser) {
        return toAjax(dyAgentUserService.updateById(dyAgentUser));
    }


    @PreAuthorize("@ss.hasPermi('modules:user:edit')")
    @Log(title = "用户封禁", businessType = BusinessType.UPDATE)
    @ApiOperation(value="用户封禁", notes="用户封禁")
    @PutMapping("/updateBannedFlag")
    public AjaxResult updateBannedFlag(@RequestBody  DyAgentUser dyAgentUser) {
        dyAgentUserService.update(new LambdaUpdateWrapper<DyAgentUser>()
                .set(DyAgentUser::getBannedFlag, dyAgentUser.getBannedFlag())
                .set(DyAgentUser::getBannedReason, dyAgentUser.getBannedReason())
                .eq(DyAgentUser::getId, dyAgentUser.getId()));


        if (1 == dyAgentUser.getBannedFlag()){
            return success("封禁成功");
        } else {
            return success("解封成功");
        }
    }



    @PreAuthorize("@ss.hasPermi('modules:user:edit')")
    @Log(title = "修改次数", businessType = BusinessType.UPDATE)
    @ApiOperation(value="修改次数", notes="修改次数")
    @PutMapping("/updateNum")
    public AjaxResult updateNum(Long agentUserId, Long num, int addSubFlag ) {

        if (addSubFlag == 0){
            return toAjax(numRecordService.addNum(agentUserId, num, "线下充值"));
        }
        if (addSubFlag == 1){
            return toAjax(numRecordService.subNum(agentUserId, num, "线下消耗"));
        }

        return error();
    }


    /**
     * 删除代理
     */
    @PreAuthorize("@ss.hasPermi('modules:user:remove')")
    @Log(title = "代理", businessType = BusinessType.DELETE)
    @ApiOperation(value="代理删除", notes="代理删除")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(dyAgentUserService.removeByIds(Arrays.asList(ids)));
    }

}
